package edu.lingnan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import edu.lingnan.annotation.Log;
import edu.lingnan.constant.LogTypeEnum;
import edu.lingnan.dto.StudentPasswordReq;
import edu.lingnan.dto.TeacherGetAllReq;
import edu.lingnan.dto.TeacherGetListReq;
import edu.lingnan.dto.TeacherPasswordReq;
import edu.lingnan.dto.result.TeacherReq;
import edu.lingnan.entity.Student;
import edu.lingnan.entity.Teacher;
import edu.lingnan.service.TeacherService;
import edu.lingnan.vo.LogStudentVo;
import edu.lingnan.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "教师控制器")
@Slf4j
public class TeacherController {
    @Resource
    private TeacherService teacherService;

    @Resource
    private HttpServletRequest request;

    // @GetMapping("/teachers")
    // public Result findAll() {
    //     List<Teacher> list = teacherService.list();
    //     return new Result(true, list, "操作成功");
    // }

    @PostMapping("/teachers")
    @ApiOperation("获得教师列表")
    public Result findAll(@RequestBody TeacherGetAllReq teacherGetAllReq) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getTStatus, 1);
        //首先判断是否有这个参数
        if (!ObjectUtils.isEmpty(teacherGetAllReq.getTCollege())) {
            wrapper.eq(Teacher::getTCollege, teacherGetAllReq.getTCollege());
        }
        //有搜索内容
        if (!ObjectUtils.isEmpty(teacherGetAllReq.getKey())) {
            String key = teacherGetAllReq.getKey();
            wrapper.and(queryWrapper -> queryWrapper.like(Teacher::getTNum, key)
                    .or().like(Teacher::getTName, key)
                    .or().like(Teacher::getTPhone, key)
                    .or().like(Teacher::getTCollege, key)
                    .or().like(Teacher::getTGrade, key));
        }
        List<Teacher> list = teacherService.list(wrapper);
        return new Result(true, list, "操作成功");
    }


    @PostMapping("/teacher/login")
    @ApiOperation("教师登录")
    @Log(module = LogTypeEnum.Login)
    public Result login(@RequestBody @ApiParam("传账号密码过来") Teacher teacher, HttpSession session) {
        //LogStudentVo logStudentVo = new LogStudentVo();
        //HashMap<String, Object> map = new HashMap<>(2);
        //map.put("t_num", teacher.getTNum());
        //map.put("t_passWord", teacher.getTPassWord());
        //Teacher login = teacherService.checkLogin(map);
        //if (login != null) {
        //    logStudentVo.setSName(login.getTName());
        //    logStudentVo.setSId(login.getTNum());
        //    session.setAttribute("loginInfo", logStudentVo);
        //    if (login.getTStatus() == 0) {
        //        return new Result(false, null, "该账号注册审核中，请耐心等待管理员审核通过后再登录");
        //    } else if (login.getTStatus() == -1) {
        //        return new Result(false, null, "该账号注册审核未通过，请联系管理员");
        //    }
        //    return new Result(true, login, "操作成功");
        //} else {
        //    return new Result(false, null, "操作失败");
        //}

        Teacher dbTeacher = teacherService.getOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTNum, teacher.getTNum()));
        if (!ObjectUtils.isNull(dbTeacher)){
            if (teacher.getTPassWord().equals(dbTeacher.getTPassWord())){
                if (dbTeacher.getTStatus() == 0){
                    return new Result(false, null, "该账号注册待审核，请联系管理员");
                }else if (dbTeacher.getTStatus() == -1){
                    return new Result(false, null, "该账号注册审核未通过，请联系管理员");
                }else {
                    LogStudentVo logStudentVo = new LogStudentVo();
                    logStudentVo.setSId(dbTeacher.getTNum()).setSName(dbTeacher.getTName());
                    request.getSession().setAttribute("loginInfo", logStudentVo);
                    return new Result(true, dbTeacher, "登录成功");
                }
            }
            return new Result(false, null, "密码不正确");
        }
        return new Result(false, null,"账号不存在");

    }

    @PostMapping("/teacher/register")
    @ApiOperation("教师注册")
    @Log(module = LogTypeEnum.Register)
    public Result register(@RequestBody Teacher teacher) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("t_num", teacher.getTNum());
        boolean b = teacherService.checkTeacher(map);
        if (b) {
            return new Result(false, 0, "该老师已经存在");
        }
        boolean save = teacherService.save(teacher);
        if (save) {
            return new Result(true, 1, "操作成功");
        } else {
            return new Result(false, 0, "操作失败");
        }
    }

    @PostMapping("/teacher/info")
    @ApiOperation("教师修改个人信息(修改之后会返回修改之后的个人信息)")
    @Log(module = LogTypeEnum.UpdateInfo)
    public Result info(@RequestBody Teacher teacher) {
        if (ObjectUtils.isEmpty(teacher)) {
            return new Result(false, null, "参数错误");
        }
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teacher::getTNum, teacher.getTNum());
        boolean update = teacherService.update(teacher, queryWrapper);
        if (update) {
            Teacher byId = teacherService.getOne(queryWrapper);
            return new Result(true, byId, "修改成功");
        }
        return new Result(false, null, "修改失败");
    }

    @PostMapping("/teacher/findAllStudentRegisterInfo")
    @ApiOperation("注册审核：查看所有教师的注册信息")
    public Result findAllStudentRegisterInfo(@RequestBody TeacherGetListReq teacherGetListReq) {
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherLambdaQueryWrapper.eq(Teacher::getTStatus, 0);
        if (!ObjectUtils.isEmpty(teacherGetListReq.getTCollege())) {
            teacherLambdaQueryWrapper.eq(Teacher::getTCollege, teacherGetListReq.getTCollege());
        }
        if (!ObjectUtils.isEmpty(teacherGetListReq.getKey())) {
            String key = teacherGetListReq.getKey();
            teacherLambdaQueryWrapper.and(queryWrapper -> queryWrapper.like(Teacher::getTNum, key)
                    .or().like(Teacher::getTName, key)
                    .or().like(Teacher::getTCollege, key)
                    .or().like(Teacher::getTPhone, key)
                    .or().like(Teacher::getTGrade, key));
        }
        List<Teacher> list = teacherService.list(teacherLambdaQueryWrapper);
        return new Result(true, list, "操作成功");
    }

    @PostMapping("/teacher/updateOrDeleteTeacherStatus")
    @ApiOperation("注册审核:更新老师状态，即注册通过或注册失败")
    public Result updateOrDeleteStudentStatus(@RequestBody TeacherReq teacherReq) {
        Teacher teacher = new Teacher()
                .setTStatus(teacherReq.getTStatus())
                .setTNum(teacherReq.getTNum());
        log.info("前端传来的数据:{}" + teacherReq);
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherLambdaQueryWrapper.eq(Teacher::getTNum, teacherReq.getTNum());
        boolean update = teacherService.update(teacher, teacherLambdaQueryWrapper);
        if (update) {
            return new Result(true, null, "操作成功");
        }
        return new Result(false, null, "操作失败");
    }

    @PutMapping("/teacher/updatePassword")
    @ApiOperation("修改教师密码")
    public Result updatePassword(@RequestBody TeacherPasswordReq teacherPasswordReq) {
        if (ObjectUtils.isEmpty(teacherPasswordReq)) {
            return new Result(false, null, "参数错误");
        }
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getTNum, teacherPasswordReq.getTNum());
        Teacher teacher = new Teacher();
        teacher.setTPassWord(teacherPasswordReq.getTPassWord());
        boolean update = teacherService.update(teacher, wrapper);
        if (update) {
            return new Result(true, 1, "更新成功");
        }
        return new Result(false, null, "更新失败");
    }

    @DeleteMapping("/teacher/{tNum}")
    public Result delete(@PathVariable("tNum") String tNum) {
        teacherService.remove(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTNum, tNum));
        return new Result(true, null, "移除成功");
    }

}
